package com.cspg.design.ui.scheme;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.cspg.design.R;
import com.cspg.design.presenter.SchemePresenter;
import com.cspg.design.ui.schemeActivity.CustomSchemeActivity;
import com.cspg.design.ui.schemeActivity.LowVoltageSchemeActivity;
import com.cspg.design.ui.schemeActivity.AerialSchemeActivity;
import com.cspg.design.ui.schemeActivity.MySchemeActivity;
import com.cspg.design.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

public class SchemeFragment extends Fragment {

    private SchemeViewModel schemeViewModel;

    private ListView mListViewSchema;

    private List<String>  mListSchema = new ArrayList<>();

    private SchemePresenter schemePresenter;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        schemeViewModel =
                ViewModelProviders.of(this).get(SchemeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_scheme, container, false);
        final TextView textView = root.findViewById(R.id.text_scheme);
        mListViewSchema = (ListView) root.findViewById(R.id.lv_schema);

        StatusBarUtil.setStatusBarColor(getActivity(),R.color.home_bg_main);
        StatusBarUtil.setLightStatusBar(getActivity(),true,false);

        initData();
        ArrayAdapter<String> mSchemaAdapter = new ArrayAdapter<String>(getActivity(),
                R.layout.listview_scheme_setting_item,R.id.item_recent_task_name,
                mListSchema.toArray(new String[mListSchema.size()]));
        mListViewSchema.setAdapter(mSchemaAdapter);
        mListViewSchema.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position)
                {
                    case 0:
                        startActivity(new Intent(getActivity(), AerialSchemeActivity.class));
                        break;
                    case 1:
                        startActivity(new Intent(getActivity(), LowVoltageSchemeActivity.class));
                        break;
                    case 2:
                        startActivity(new Intent(getActivity(), CustomSchemeActivity.class));
                        break;
                    case 3:
                        startActivity(new Intent(getActivity(), MySchemeActivity.class));
                        break;
                    default:
                        break;
                }
            }
        });
        schemeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        schemePresenter = new SchemePresenter(getActivity(),null,null);
        schemePresenter.getHighVoltageScheme();
        schemePresenter.getLowVoltageScheme();

        return root;
    }

    private void initData(){
        mListSchema.add("架空方案");
        mListSchema.add("低压方案");
        mListSchema.add("自定义方案");
        mListSchema.add("我的方案");

    }
}